﻿@using Telerik.Web.Mvc.UI;
@model PollModel

@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
@Html.SmartStore().TabStrip().Name("poll-edit").Style(TabsStyle.Material).Items(x =>
{
    x.Add().Text(T("Admin.ContentManagement.Polls.Info").Text).Content(TabInfo()).Selected(true);
    x.Add().Text(T("Admin.ContentManagement.Polls.Answers").Text).Content(TabAnswers());
    x.Add().Text(T("Common.Voting").Text).Content(TabVoting());
    
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new TabStripCreated(x, "poll-edit", this.Html, this.Model));
})

@helper TabInfo()
{        
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.LanguageId)
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.LanguageId, Model.AvailableLanguages)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Name)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SystemKeyword)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SystemKeyword)
                @Html.ValidationMessageFor(model => model.SystemKeyword)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Published)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Published)
                @Html.ValidationMessageFor(model => model.Published)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.ShowOnHomePage)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.ShowOnHomePage)
                @Html.ValidationMessageFor(model => model.ShowOnHomePage)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.AllowGuestsToVote)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.AllowGuestsToVote)
                @Html.ValidationMessageFor(model => model.AllowGuestsToVote)
            </td>
        </tr>
        <tr>
            <td class="adminTitle"> 
                @Html.SmartLabelFor(model => model.DisplayOrder)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.DisplayOrder)
                @Html.ValidationMessageFor(model => model.DisplayOrder)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.StartDate)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.StartDate)
                @Html.ValidationMessageFor(model => model.StartDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.EndDate)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.EndDate)
                @Html.ValidationMessageFor(model => model.EndDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SelectedStoreIds)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SelectedStoreIds)
                @Html.ValidationMessageFor(model => model.SelectedStoreIds)
            </td>
        </tr>
    </table>
}

@helper TabAnswers()
{
    if (Model.Id > 0)
    {
		<div>
			@(Html.Telerik().Grid<PollAnswerModel>()
					.Name("answers-grid")
					.DataKeys(x =>
					{
						x.Add(y => y.Id).RouteKey("Id");
						x.Add(y => y.PollId).RouteKey("pollId");
					})
					.Columns(columns =>
					{
						columns.Bound(x => x.Name)
							.Width("60%");
						columns.Bound(x => x.NumberOfVotes)
							.ReadOnly()
							.Width("10%")
                            .Centered();
						columns.Bound(x => x.DisplayOrder1)
							.Width("10%")
                            .Centered();
						columns.Command(commands =>
						{
							commands.Edit().Localize(T);
							commands.Delete().Localize(T);
						})
						.Width("20%")
						.HtmlAttributes(new { align = "right" }); ;
					})
					.ToolBar(x => x.Insert())
					.Editable(x =>
					{
						x.Mode(GridEditMode.InLine);
					})
					.DataBinding(dataBinding =>
					{
						dataBinding.Ajax().Select("PollAnswers", "Poll", new { pollId = Model.Id })
							.Update("PollAnswerUpdate", "Poll")
							.Delete("PollAnswerDelete", "Poll")
							.Insert("PollAnswerAdd", "Poll", new { pollId = Model.Id });
					})
					.EnableCustomBinding(true))
		</div>
    }
    else
    {
		@T("Admin.ContentManagement.Polls.Answers.SaveBeforeEdit")
    }
}

@helper TabVoting()
{
    if (Model.Id > 0)
    {
        <div>
            @(Html.Telerik().Grid<PollVotingRecordModel>()
                .Name("voting-records-grid")
                .DataKeys(x =>
                {
                    x.Add(y => y.Id).RouteKey("Id");
                })
                .Columns(columns =>
                {
			        columns.Bound(x => x.Email)
				        .Template(x => Html.ActionLink(x.Email, "Edit", "Customer", new { id = x.CustomerId }, new { }))
				        .ClientTemplate("<a href=\"" + Url.Content("~/Admin/Customer/Edit/") + "<#= CustomerId #>\"><#= Email #></a>");
			        columns.Bound(x => x.Username)
				        .Visible(Model.UsernamesEnabled);
			        columns.Bound(x => x.FullName);
                    columns.Bound(x => x.AnswerName);
                    columns.Bound(x => x.CreatedOn);
                })
                .Pageable(settings => settings.PageSize(Model.GridPageSize).Position(GridPagerPosition.Both))
                .DataBinding(dataBinding => dataBinding.Ajax().Select("VotingRecords", "Poll", new { pollId = Model.Id }))
                .EnableCustomBinding(true))
        </div>
    }
    else
    {
		@T("Admin.ContentManagement.Polls.Answers.SaveBeforeEdit")
    }
}